package com.hnichr.listener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;


@Component
public class RabbitTemplateConfigListener implements ApplicationListener<ApplicationReadyEvent> {

    private static final Logger logger = LoggerFactory.getLogger(RabbitTemplateConfigListener.class);

    @Autowired
    private ApplicationContext applicationContext;
    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        RabbitTemplate rabbitTemplate = applicationContext.getBean(RabbitTemplate.class);
        rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
            logger.info("ack:{},cause:{}", ack, cause);

        });

        rabbitTemplate.setReturnsCallback(returnedMessage -> {
            logger.info("returnedMessage:replyCode:{},replyText:{},message:{},exchange:{},routingKey:{}", returnedMessage.getMessage(),returnedMessage.getExchange(),returnedMessage.getRoutingKey(),
                    returnedMessage.getReplyCode(),returnedMessage.getReplyText());
        });
    }
}
